<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>数据模型配置</title>
        <{$showjs}>
        <link rel="stylesheet" type="text/css" href="/Plug-in/ext/resources/css/xtheme-slate.css" />
        <link rel="stylesheet" type="text/css" href="../css/main1.css" />
        <script type="text/javascript" src="/Plug-in/ext/Ext.grid.CheckColumn.js"></script>
        <script type="text/javascript" src="/Plug-in/ext/Ext.Store.Sort.js"></script>
        <script type="text/javascript" src="/Plug-in/ext/Ext.form.Property.js"></script>
        <style>
            .f_new{background-image:url(../img/table_row_insert.png)!important;}
            .f_del{background-image:url(../img/table_row_delete.png)!important;}
            .f_refresh{background-image:url(../img/table_refresh.png)!important;}
            .f_save{background-image:url(../img/table_save.png)!important;}
            .f_edit{background-image:url(../img/table_edit.png)!important;}
            .f_cfg{background-image:url(../img/table_multiple.png)!important;}
            .f_zj{background-image:url(../img/b_primary.png)!important;}
            .f_wy{background-image:url(../img/b_unique.png)!important;}
            .f_sy{background-image:url(../img/b_index.png)!important;}
            .f_qw{background-image:url(../img/b_ftext.png)!important;}
            .i_filed{background-image:url(../img/application_form.png)!important;}

        </style>
        <script language="javascript">
            Ext.onReady(function(){
                Ext.QuickTips.init();
                var myMask = new Ext.LoadMask(Ext.getBody(), {
                    msg: '请稍候...',
                    removeMask: true //完成后移除
                });
                var fields = [		{name : 'id'},
                    {name : 'id_mirror'},
                    {name : 'name'},
                    {name : 'tabname'},
                    {name : 'extend_name'},
                    {name : 'created'}
                    //{name : 'field_num'}
                ];
                //---------------------------------------------first grid---------------------------------------------
                var reader = new Ext.data.JsonReader(
                {
                }, fields);
                var proxy = new Ext.data.HttpProxy({
                    url : 'tablep.php'
                });
                var cols = [
                    { id : 'name', header: "名称", sortable: true, dataIndex: 'name',width:60},
                    { id : 'tabname', header: "表名", sortable: true, dataIndex: 'tabname',width:60},
                    { id : 'created', header: "是否已创建", sortable: true, dataIndex: 'created',width:60,renderer:function(val){
                            return val==1?'<font color=green><center><b>√</b></center></font>':'<font color=red><center><b>×</b></center></font>';
                        }}
                ];

                var firstGridStore = new Ext.data.Store({
                    baseParams: {op: 'grid_data'},
                    proxy : proxy,
                    reader : reader
                });
                firstGridStore.load();
                var firstGrid = new Ext.grid.GridPanel({
                    ddGroup          : 'secondGridDDGroup',
                    iconCls:'f_edit',
                    store            : firstGridStore,
                    columns          : cols,
                    enableDragDrop   : true,
                    stripeRows       : true,
                    autoExpandColumn : 'name',
                    width            : 230,
                    region           : 'west',
                    title            : '现有数据模型',
                    frame			 : false
                });
                //-----------------------------------------------------second grid-------------------------------------
                var fm = Ext.form,Ed = Ext.grid.GridEditor;
                var fields = [
                    {name: 'id', mapping : 'id'},
                    {name: 'field_name', mapping : 'field_name'},
                    {name: 'name', mapping : 'name'},
                    {name: 'property', mapping : 'property'},
                ];
                var comboStore =  new Ext.data.JsonStore({
                    id:'comboStore',
                    url: 'field.php',
                    root: 'rows',
                    fields: ['name','value','param','method']
                });
                var update_Data=0;
                function getData(bGrid_event){
                    update_Data=bGrid_event.record.data;
                }
                function updateDB(oGrid_event){
                    var	data=oGrid_event.record.data;
                    var _originalfield='';
                    if(oGrid_event.field=='field_name'){
                        _originalfield=oGrid_event.originalValue;
                    }else{
                        _originalfield=data.field_name;
                    }
                    myMask.show();
                    Ext.Ajax.request(
                    {
                        waitMsg:'正在提交....',
                        method:'POST',
                        params:{require:'item_update',data:Ext.encode(data),parent_id:select_row,selectfield:oGrid_event.field,originalfield:_originalfield},
                        url:'field.php',
                        success:function(form, action) {
                            myMask.hide();
                            if(form.responseText!=''){
                                Ext.Msg.alert('提醒',form.responseText);
                            }

                        },
                        failure: function(form, action) {
                        }
                    }
                );
                }
                comboStore.load({params:{require: 'property'}});
                var property_combo = new fm.ComboBox({
                    id : 'catalog_',
                    name : 'catalog_',
                    emptyText  :'选择一个类型',
                    fieldLabel : '类型',
                    allowBlank : true,
                    store:comboStore,
                    selectOnFocus:true,
                    mode:'local',
                    readOnly :true,
                    displayField: "value",
                    forceSelection: true,
                    triggerAction: "all"
                });
                var cm = new Ext.grid.ColumnModel([{
                        id : 'field_name',
                        header: "字段名",
                        dataIndex: 'field_name',
                        width: 80,
                        editor: new fm.TextField({
                            allowBlank: false
                        })
                    },{
                        id : 'name',
                        header: "字段注解",
                        dataIndex: 'name',
                        width: 80,
                        editor: new fm.TextField({
                            allowBlank: false
                        })
                    },{
                        id : 'property',
                        header: "属性",
                        dataIndex: 'property',
                        width: 100,
                        editor:property_combo
                    }
                ]);

                cm.defaultSortable = true;
                var secondGridStore = new Ext.data.JsonStore({
                    fields : fields,
                    root   : 'records',
                    sortInfo: {field: 'field_name',direction: 'DESC'}
                });
                var secondGrid = new Ext.grid.EditorGridPanel({
                    store            : secondGridStore,
                    cm				 : cm,
                    autoExpandColumn : 'name',
                    region           : 'center',
                    frame			 : false,
                    clicksToEdit	 : 1,
                    iconCls:'f_cfg',
                    title            : '字段配置',
                    listeners: {
                        "beforeedit":{
                            fn: getData,
                            scope: this
                        },
                        "afterEdit": {
                            fn: updateDB,
                            scope: this
                        }

                    }
                });
                secondGrid.addListener('rowcontextmenu', rightClickFn);
                function field_add(){
                    if(select_row==0){
                        Ext.Msg.alert('提示','请选择拖拽一个数据库');
                        return false;
                    }
                    var win = new Ext.Window({
                        id			:'webbase_config',
                        title    	: '添加新字段',
                        width       : 400,
                        height      : 180,
                        iconCls:'f_new',
                        constrain	: true,
                        closeAction :'close',
                        html		: '<div id="formpanel"></div>'
                    });

                    win.show();
                    var property_combo_form = new fm.ComboBox({
                        id : 'property',
                        name : 'property',
                        emptyText  :'选择一个类型',
                        fieldLabel : '类型',
                        allowBlank : false,
                        store:comboStore,
                        selectOnFocus:true,
                        mode:'local',
                        readOnly :true,
                        displayField: "value",
                        forceSelection: true,
                        triggerAction: "all"
                    });
                    var form = new fm.FormPanel({
                        renderTo   : 'formpanel',
                        title      : false,
                        border	   : false,
                        frame	   : true,
                        labelWidth : 100,
                        layout	   :'form',
                        bodyStyle:'padding:5px;',
                        autoHeight:true,
                        defaults: {width: 200},
                        defaultType: 'textfield',
                        bodyStyle:'padding:10px;',
                        items	   :[
                            {
                                fieldLabel: '字段名',
                                id:'field_name',
                                name: 'field_name',
                                allowBlank:false
                            },{
                                fieldLabel: '字段注解',
                                id:'name',
                                name: 'name',
                                allowBlank:true
                            },property_combo_form]
                        ,
                        buttons: [{
                                text: '保存',
                                handler: function(){

                                    if(form.getForm().isValid()){
                                        form.getForm().doAction('submit',{
                                            url:'field.php',
                                            method:'POST',
                                            params:{require:'item_add',id:select_row},
                                            waitMsg:'正在提交....',
                                            success:function(form,action){
                                                var ret = Ext.decode(action.result.msg);
                                                var config=new blankRecord(ret);
                                                secondGridStore.add(config);
                                                win.close();
                                            },
                                            //提交失败的回调函数
                                            failure:function(form,action){
                                                Ext.Msg.alert('错误',action.result.msg);
                                            }
                                        });
                                    }
                                }
                            },{
                                text: '重置',
                                handler: function(){
                                    form.getForm().reset();
                                }
                            }],
                            listeners:{
                                render :function(){
                                    Ext.getCmp('field_name').focus(true,true);
                                }
                            }
                    });
                }
                function rightClickFn(client, rowIndex, e) {
                    e.preventDefault();
                    var menus = new Ext.menu.Menu
                    ([
                        {
                            xtype:"button",text:"新建",pressed:true,
                            iconCls:'f_new',
                            handler:field_add
                        },
                        {
                            xtype:"button",text:"删除",pressed:true,
                            iconCls:'f_del',
                            handler:function(){
                                Ext.MessageBox.confirm("系统提示","删除过后不能恢复,你确认删除当前字段吗?",function(_btn){
                                    if(_btn=="yes"){
                                        var record=secondGridStore.getAt(rowIndex);
                                        var field_name =record.data.field_name;
                                        myMask.show();
                                        Ext.Ajax.request({
                                            url : 'field.php',
                                            params : {require:'item_del',id:select_row,field:field_name},
                                            success:function(response){
                                                if (response.responseText=='ok') {
                                                    secondGridStore.remove(record);
                                                    myMask.hide();
                                                } else {
                                                    Ext.Msg.alert('数据编辑',response.responseText);
                                                }
                                            },
                                            //提交失败的回调函数
                                            failure:function(){
                                                Ext.Msg.alert('错误','数据提交错误！');
                                            }
                                        });
                                    }
                                });
                            }
                        },
                        {
                            xtype:"button",text:"主键",pressed:true,
                            iconCls:'f_zj',
                            handler:function(){
                                Ext.MessageBox.confirm("系统提示","是主键将去除,非主键将设为主键,你确认要操作此字段么?",function(_btn){
                                    if(_btn=="yes"){
                                        var record=secondGridStore.getAt(rowIndex);
                                        var field_name =record.data.field_name;
                                        myMask.show();
                                        Ext.Ajax.request({
                                            url : 'field.php',
                                            params : {require:'item_zj',id:select_row,field:field_name},
                                            success:function(response){
                                                if (response.responseText=='ok') {
                                                    secondGridStore.remove(record);
                                                    myMask.hide();
                                                } else {
                                                    myMask.hide();
                                                    Ext.Msg.alert('数据编辑',response.responseText);
                                                }
                                            },
                                            //提交失败的回调函数
                                            failure:function(){
                                                Ext.Msg.alert('错误','数据提交错误！');
                                            }
                                        });
                                    }
                                });
                            }
                        },
                        {
                            xtype:"button",text:"索引",pressed:true,
                            iconCls:'f_sy',
                            handler:function(){
                                Ext.MessageBox.confirm("系统提示","是索引将去掉,非索引将设为索引,你确认要操作此字段么?",function(_btn){
                                    if(_btn=="yes"){
                                        var record=secondGridStore.getAt(rowIndex);
                                        var field_name =record.data.field_name;
                                        myMask.show();
                                        Ext.Ajax.request({
                                            url : 'field.php',
                                            params : {require:'item_sy',id:select_row,field:field_name},
                                            success:function(response){
                                                if (response.responseText=='ok') {
                                                    secondGridStore.remove(record);
                                                    myMask.hide();
                                                } else {
                                                    myMask.hide();
                                                    Ext.Msg.alert('数据编辑',response.responseText);
                                                }
                                            },
                                            //提交失败的回调函数
                                            failure:function(){ 
                                                Ext.Msg.alert('错误','数据提交错误！');
                                            }
                                        });
                                    }
                                });
                            }
                        },
                        {
                            xtype:"button",text:"唯一",pressed:true,
                            iconCls:'f_wy',
                            handler:function(){
                                Ext.MessageBox.confirm("系统提示","是唯一将去除,非唯一将设为唯一,你确认要操作此字段么?",function(_btn){
                                    if(_btn=="yes"){
                                        var record=secondGridStore.getAt(rowIndex);
                                        var field_name =record.data.field_name;
                                        myMask.show();
                                        Ext.Ajax.request({
                                            url : 'field.php',
                                            params : {require:'item_wy',id:select_row,field:field_name},
                                            success:function(response){
                                                if (response.responseText=='ok') {
                                                    secondGridStore.remove(record);
                                                    myMask.hide();
                                                } else {
                                                    myMask.hide();
                                                    Ext.Msg.alert('数据编辑',response.responseText);
                                                }
                                            },
                                            //提交失败的回调函数
                                            failure:function(){
                                                Ext.Msg.alert('错误','数据提交错误！');
                                            }
                                        });
                                    }
                                });
                            }
                        },
                        {
                            xtype:"button",text:"全文搜索",pressed:true,
                            iconCls:'f_qw',
                            handler:function(){
                                Ext.MessageBox.confirm("系统提示","是全文搜索将去除,非全文搜索将设为全文搜索,你确认要操作此字段么?",function(_btn){
                                    if(_btn=="yes"){
                                        var record=secondGridStore.getAt(rowIndex);
                                        var field_name =record.data.field_name;
                                        myMask.show();
                                        Ext.Ajax.request({
                                            url : 'field.php',
                                            params : {require:'item_qw',id:select_row,field:field_name},
                                            success:function(response){
                                                if (response.responseText=='ok') {
                                                    secondGridStore.remove(record);
                                                    myMask.hide();
                                                } else {
                                                    myMask.hide();
                                                    Ext.Msg.alert('数据编辑',response.responseText);
                                                }
                                            },
                                            //提交失败的回调函数
                                            failure:function(){
                                                Ext.Msg.alert('错误','数据提交错误！');
                                            }
                                        });
                                    }
                                });
                            }
                        }
			
                    ]);
                    menus.showAt(e.getXY());
                }


                var	 foot=new Ext.Panel({
                    region:'south',
                    height:30,
                    bbar:['->',{xtype: 'button',text:'新建字段',iconCls:'f_new',handler:field_add},'-',{xtype: 'button',iconCls:'i_datebase',text:'数据表管理',handler:function(){
                                window.location.href='./table.php?op=showtp';
                            }}]
                });
                var Viewport = new Ext.Viewport({
                    layout   : 'border',
                    items    : [
                        firstGrid,
                        secondGrid,
                        foot
                    ]

                });
                var select_row=0;
                var blankRecord =  Ext.data.Record.create(fields);
                var secondGridDropTargetEl = secondGrid.getView().el.dom.childNodes[0].childNodes[1]
                var destGridDropTarget = new Ext.dd.DropTarget(secondGridDropTargetEl, {
                    ddGroup    : 'secondGridDDGroup',
                    copy       : false,
                    notifyDrop : function(ddSource, e, data){
                        myMask.show();
                        secondGridStore.removeAll();
                        var selectedRecord = ddSource.dragData.selections[0];
                        select_row=selectedRecord.data.id;
                        Ext.Ajax.request(
                        {
                            waitMsg:'正在提交....',
                            method:'POST',
                            params:{require:'field_echo',parent_id:select_row},
                            url:'field.php',
                            success:function(form, action) {
                                //Ext.Msg.alert('登陆成功',form.responseText);
                                myMask.hide();
                                if(form.responseText!='false'){
                                    var field_data=Ext.decode(form.responseText);
                                    Ext.each(field_data,function(v,i,a){
                                        var config=new blankRecord(v);
                                        secondGridStore.add(config);
                                    });
                                }
                            },
                            failure: function(form, action) {
                                Ext.Msg.alert('失败','服务器端没有响应');
                            }
                        }
                    );
                        return(true);
                    }
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>